package com.hulei.studyproject.controller;

import com.hulei.studyproject.druid.annotation.Master;
import com.hulei.studyproject.druid.annotation.Slave;
import com.hulei.studyproject.entity.User;
import com.hulei.studyproject.service.IUserService;
import com.hulei.studyproject.spel.SpelGetParam;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Title: QueryController
 * @Description: TODO
 * @author: hulei
 * @date: 2023/7/31 10:40
 * @Version: 1.0
 */
@RestController
@RequestMapping("/nacos")
@RequiredArgsConstructor
public class QueryController {

    private final JdbcTemplate jdbcTemplate;

    private final IUserService userService;

    @GetMapping("/{info}/queryUsers")
    @SpelGetParam(param = "#info")
    @Master
    public Object getInfo(@PathVariable("info")String info){
        return jdbcTemplate.queryForList("select * from items");
    }

    @PostMapping("/postUser")
    @SpelGetParam(param = "#user.name")
    @Master
    public Object insertUser(@RequestBody User user){
        String sql = "insert into user(id,name) values (?,?)";
        return jdbcTemplate.update(sql, user.getId(), user.getName());
    }

    @PostMapping("/insertUser")
    @Master
    public void insertUser(){
        userService.insert();
    }

    @GetMapping("/nacosUser")
    @Slave
    public Object getNacosUser(){
        return jdbcTemplate.queryForList("select * from users");
    }
}
